//
//  NSAttributedString+Safe.m
//  DebugDemo
//
//  Created by XFB on 2018/11/9.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "NSAttributedString+Safe.h"
#import "NSObject+Swizzling.h"

@implementation NSAttributedString (Safe)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(addAttributes:range:)
                                                       withSwizzledSelector:@selector(zc_safeAddAttribute:range:)];
        
        [objc_getClass("NSConcreteMutableAttributedString") swizzleSelector:@selector(initWithString:)
                                                       withSwizzledSelector:@selector(zc_safeInitWithString:)];
        [objc_getClass("NSConcreteMutableAttributedString") swizzleSelector:@selector(initWithString:attributes:)
                                                       withSwizzledSelector:@selector(zc_safeInitWithString:attributes:)];
        [objc_getClass("NSConcreteMutableAttributedString") swizzleSelector:@selector(addAttribute:value:range:)
                                                       withSwizzledSelector:@selector(zc_safeAddAttribute:value:range:)];
        [objc_getClass("NSConcreteMutableAttributedString") swizzleSelector:@selector(addAttributes:range:)
                                                       withSwizzledSelector:@selector(zc_safeAddAttributes:range:)];
        [objc_getClass("NSConcreteMutableAttributedString") swizzleSelector:@selector(setAttributes:range:)
                                                       withSwizzledSelector:@selector(zc_safeSetAttributes:range:)];
    });
}


- (void)zc_safeAddAttribute:(NSAttributedStringKey)name range:(NSRange)range
{
    if (!range.length) {
        [self zc_safeAddAttribute:name range:range];
    } else {
        if (range.location + range.length <= self.length) {
            [self zc_safeAddAttribute:name range:range];
        }
    }
}

- (id)zc_safeInitWithString:(NSString *)aString
{
    if(!aString) return @"";
    return [self zc_safeInitWithString:aString];
}

- (id)zc_safeInitWithString:(NSString *)aString attributes:(NSAttributedStringKey)name
{
    if(!aString) return @"";
    return [self zc_safeInitWithString:aString attributes:name];
}

- (void)zc_safeAddAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range
{
    if (!range.length) {
        [self zc_safeAddAttribute:name value:value range:range];
    } else {
        if (range.location + range.length <= self.length) {
            [self zc_safeAddAttribute:name value:value range:range];
        }
    }
}

- (void)zc_safeAddAttributes:(NSAttributedStringKey)name range:(NSRange)range
{
    if (!range.length) {
        [self zc_safeAddAttributes:name range:range];
    } else {
        if (range.location + range.length <= self.length) {
            [self zc_safeAddAttributes:name range:range];
        }
    }
}

- (void)zc_safeSetAttributes:(NSAttributedStringKey)name range:(NSRange)range
{
    if (!range.length) {
        [self zc_safeSetAttributes:name range:range];
    } else {
        if (range.location + range.length <= self.length) {
            [self zc_safeSetAttributes:name range:range];
        }
    }
}



@end
